//---------------------------------------------------------------------------------
// Copyright (c) 2008 VoiceLynx Project
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
//     1. The origin of this software must not be misrepresented; you must not
//        claim that you wrote the original software. If you use this software
//        in a product, an acknowledgment in the product documentation would be
//        appreciated but is not required.
//
//     2. Altered source versions must be plainly marked as such, and must not be
//        misrepresented as being the original software.
//
//     3. This notice may not be removed or altered from any source
//        distribution.
//---------------------------------------------------------------------------------


#include "UI_ServersDlg.h"

UI_ServersDlgExt::UI_ServersDlgExt(wxWindow* parent, VLServerSettings &settings)
: UI_ServersDlg(parent),
  mSettings(settings),
  mDirty(false),
  mSelectedIsNew(false),
  mDirtyDetection(true)
{
  RefreshList();
}

void UI_ServersDlgExt::OnServersSelectionChange( wxCommandEvent& event )
{
  if (CheckIfDirty())
  {
    RefreshRightSide();
    mSelectedIsNew = false;
    mDirty = false;

    mSelectedName = m_lstServers->GetStringSelection();
  }
  
}

void UI_ServersDlgExt::OnAdd( wxCommandEvent& event )
{
  // Figure out an appropriate new name - we need to do
  // this BEFORE we possibly remove the previous "new" entry
  int count = 0;
  wxString name;

  do
  {
    ++count;
    if (count == 1)
    {
      name = wxT("New Server");
    }
    else
    {
      name = wxString::Format(wxT("New Server (%d)"), count);
    }
  } while (m_lstServers->GetStrings().Index(name) != -1);

  if (CheckIfDirty())
  {
    VLServerSettingsEntry server;

    server.name = name;
    server.host = wxT("");
    mSettings.setServer(server);

    RefreshList();

    m_lstServers->SetStringSelection(name);

    RefreshRightSide();

    mSelectedIsNew = true;
    mSelectedName = name;

    MarkAsDirty();
  }
}

void UI_ServersDlgExt::OnDelete( wxCommandEvent& event )
{
  wxString msg = wxString::Format(
    wxT("Are you sure you want to delete %s?"), 
    m_lstServers->GetStringSelection());

  int result = wxMessageBox(msg, wxT("VoiceLynx"), wxYES_NO, this);

  if (result == wxYES)
  {
    mSettings.deleteServer(m_lstServers->GetStringSelection());

    m_lstServers->Deselect(m_lstServers->GetSelection());
    mDirty = false;

    RefreshList();
    RefreshRightSide();
  }
}

void UI_ServersDlgExt::OnNameChanged( wxCommandEvent& event )
{
	if (mDirtyDetection)
  {
    MarkAsDirty();
  }
}

void UI_ServersDlgExt::OnServerChanged( wxCommandEvent& event )
{
  if (mDirtyDetection)
  {
    MarkAsDirty();
  }
}

void UI_ServersDlgExt::OnPortChanged( wxCommandEvent& event )
{
  if (mDirtyDetection)
  {
    MarkAsDirty();
  }
}

void UI_ServersDlgExt::OnSave( wxCommandEvent& event )
{
	Save();
}

void UI_ServersDlgExt::OnCancel( wxCommandEvent& event )
{
  Cancel();
}

void UI_ServersDlgExt::OnClose( wxCommandEvent& event )
{
  if (CheckIfDirty())
  {
    Destroy();
  }
}

void UI_ServersDlgExt::RefreshList()
{
  wxArrayString serverNames;
  mSettings.getServerNames(serverNames);

  m_lstServers->Clear();
  m_lstServers->Append(serverNames);
}

void UI_ServersDlgExt::RefreshRightSide()
{
  if (m_lstServers->GetSelection() == -1)
  {
    mDirtyDetection = false;

    m_txtName->SetValue(wxT(""));
    m_txtServer->SetValue(wxT(""));
    m_txtPort->SetValue(wxString::Format(wxT("%d"), VL_DEFAULT_PORT));

    mDirtyDetection = true;

    m_txtName->Enable(false);
    m_txtServer->Enable(false);
    m_txtPort->Enable(false);

    m_btnSave->Enable(false);
    m_btnCancel->Enable(false);
    m_btnDelete->Enable(false);
  }
  else
  {
    VLServerSettingsEntry server;

    // Get the server settings
    mSettings.getServer(m_lstServers->GetStringSelection(), server);

    // Set the text fields
    mDirtyDetection = false;
    m_txtName->SetValue(server.name);
    m_txtServer->SetValue(server.host);
    m_txtPort->SetValue(wxString::Format(wxT("%d"), server.port));
    mDirtyDetection = true;

    // Enable everything
    m_btnSave->Enable(false);
    m_btnDelete->Enable();
    m_btnCancel->Enable(false);

    m_txtName->Enable();
    m_txtServer->Enable();
    m_txtPort->Enable();
  }
}

void UI_ServersDlgExt::MarkAsDirty(bool isDirty)
{
  m_btnSave->Enable(isDirty);
  m_btnCancel->Enable(isDirty);

  mDirty = isDirty;
}

bool UI_ServersDlgExt::CheckIfDirty()
{
  bool shouldContinue = true;

  if (mDirty)
  {
    wxString msg = wxString::Format(
      wxT("Would you like to save changes to %s?"), 
      mSelectedName);

    int result = wxMessageBox(msg, wxT("VoiceLynx"), wxYES_NO | wxCANCEL, this);

    if (result == wxYES)
    {
      Save();
    }
    else if (result == wxNO)
    {
      Cancel();
    }
    else
    {
      m_lstServers->SetStringSelection(mSelectedName);
      shouldContinue = false;
    }
  }

  return shouldContinue;
}

void UI_ServersDlgExt::Cancel()
{
  if (mSelectedIsNew)
  {
    mSettings.deleteServer(mSelectedName);
  }

  mDirty = false;
  RefreshList();
  RefreshRightSide();
}

void UI_ServersDlgExt::Save()
{
  VLServerSettingsEntry server;

  server.name = m_txtName->GetValue();
  server.host = m_txtServer->GetValue();
   
  unsigned long val = -1;
  bool conversionGood = m_txtPort->GetValue().ToULong(&val);

  if (conversionGood && val < 65535)
  {
    server.port = (VLU16) val;
  }
  else
  {
    conversionGood = false;
  }

  if (conversionGood == false)
  {
    //TODO: We need to handle this error condition
  }

  if (mSelectedName != server.name)
  {
    mSettings.deleteServer(mSelectedName);
  }

  mSettings.setServer(server);

  mDirty = false;
  RefreshList();

  m_lstServers->SetStringSelection(server.name);

  RefreshRightSide();
}
